import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.font.NumericShaper;
import java.awt.geom.Rectangle2D;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import javax.swing.JFrame;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisState;
import org.jfree.chart.axis.LogAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTick;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.axis.TickType;
import org.jfree.chart.axis.TickUnit;
import org.jfree.chart.axis.TickUnitSource;
import org.jfree.chart.labels.CategoryItemLabelGenerator;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.labels.StandardXYItemLabelGenerator;
import org.jfree.chart.labels.XYItemLabelGenerator;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.Range;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RectangleEdge;
import org.jfree.ui.RefineryUtilities;
import org.jfree.ui.TextAnchor;

/**
 * A simple demonstration application showing how to create a line chart using
 * data from an {@link XYDataset}.
 *
 */
public class LineChartDemo6 extends ApplicationFrame {

	/**
	 * Creates a new demo.
	 *
	 * @param title
	 *            the frame title.
	 */
	public LineChartDemo6(final String title) {

		super(title);

		final XYDataset dataset = createDataset();
		final JFreeChart chart = createChart(dataset);
		final ChartPanel chartPanel = new ChartPanel(chart);
		chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
		setContentPane(chartPanel);

	}

	/**
	 * Creates a sample dataset.
	 * 
	 * @return a sample dataset.
	 */
	private XYDataset createDataset() {
		 

		final XYSeries series1 = new XYSeries(
				" Accumulated minimum guaranteed benefit");
		//series1.add(1, 2500);
		//series1.add(2, 13000);
		//series1.add(3, 55000);
		//series1.add(4, 100000);
		//series1.add(5, 380000);
		//series1.add(6, 1580000);

		// series1.add(7,140000);
		// series1.add(8,160000);
		// series1.add(9,180000);
		// series1.add(10,2000000);
		series1.add(1,20000);
		series1.add(2,40000);
		series1.add(3,60000);
		series1.add(4,80000);
		series1.add(5,100000);
		series1.add(6,120000);
		series1.add(7,140000);
		series1.add(8,160000);
		series1.add(9,180000);
		series1.add(10,2000000);


		final XYSeries series2 = new XYSeries(" Accumulated estimated benefit");
		//series2.add(1, 2500);
		//series2.add(2, 13885);
		//series2.add(3, 60880);
		//series2.add(4, 114708);
		//series2.add(5, 675665);
		//series2.add(6, 1935770);
		//series2.add(6, 1935000);

		// series2.add(7,155000);
		// series2.add(8,190000);
		// series2.add(9,225000);
		// series2.add(10,2210000);
		series2.add(1,20000);
		series2.add(2,40000);
		series2.add(3,60000);
		series2.add(4,80000);
		series2.add(5,100000);
		series2.add(6,120000);
		series2.add(7,155000);
		series2.add(8,190000);
		series2.add(9,225000);
		series2.add(10,2210000);


		final XYSeries series3 = new XYSeries("Accumulated maximum benefit");
		/*
		series3.add(1, 2500);
		series3.add(2, 14062);
		series3.add(3, 62056);
		series3.add(4, 117649);
		series3.add(5, 734798);
		series3.add(6, 2006924);*/

		// series3.add(7,158000);
		// series3.add(8,196000);
		// series3.add(9,234000);
		// series3.add(10,2252000);
		series3.add(1,20000);
		series3.add(2,40000);
		series3.add(3,60000);
		series3.add(4,80000);
		series3.add(5,100000);
		series3.add(6,120000);
		series3.add(7,158000);
		series3.add(8,196000);
		series3.add(9,234000);
		series3.add(10,2252000);


		final XYSeriesCollection dataset = new XYSeriesCollection();
		dataset.addSeries(series1);
		dataset.addSeries(series2);
		dataset.addSeries(series3);

		return dataset;

	}

	/**
	 * Creates a chart.
	 * 
	 * @param dataset
	 *            the data for the chart.
	 * 
	 * @return a chart.
	 */
	private JFreeChart createChart(final XYDataset dataset) {

		// create the chart...
		final JFreeChart chart = ChartFactory.createXYLineChart(
				"Accumulated benifit left on deposit", // chart title
				"X", // x axis label
				"Y", // y axis label
				dataset, // data
				PlotOrientation.VERTICAL, true, // include legend
				true, // tooltips
				false // urls
				);

		// NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
		chart.setBackgroundPaint(Color.white);

		/*
		 * final StandardLegend legend = (StandardLegend) chart.getLegend();
		 * legend.setDisplaySeriesShapes(true);
		 */

		// get a reference to the plot for further customisation...
		final XYPlot plot = chart.getXYPlot();
		plot.setBackgroundPaint(Color.white);
		// plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0));
		plot.setDomainGridlinePaint(Color.lightGray);
		plot.setRangeGridlinePaint(Color.lightGray);
		plot.getRangeAxis().setVisible(false);

		final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
		renderer.setSeriesLinesVisible(0, true);
		renderer.setSeriesLinesVisible(1, true);
		renderer.setSeriesLinesVisible(2, true);
		renderer.setSeriesShapesVisible(0, false);
		renderer.setSeriesShapesVisible(1, false);
		renderer.setSeriesShapesVisible(2, false);
		renderer.setBaseItemLabelsVisible(true);
		renderer.setBaseItemLabelGenerator(new LegendXYItemLabelGenerator(plot
				.getLegendItems()));
		plot.setRenderer(renderer);

		// change the auto tick unit selection to integer units only...
		//final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
		//rangeAxis.setTickUnit(new NumberTickUnit(1000));
		//rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
		// OPTIONAL CUSTOMISATION COMPLETED.

		return chart;

	}

	// ****************************************************************************
	// * JFREECHART DEVELOPER GUIDE *
	// * The JFreeChart Developer Guide, written by David Gilbert, is available
	// *
	// * to purchase from Object Refinery Limited: *
	// * *
	// * http://www.object-refinery.com/jfreechart/guide.html *
	// * *
	// * Sales are used to provide funding for the JFreeChart project - please *
	// * support us so that we can continue developing free software. *
	// ****************************************************************************

	/**
	 * Starting point for the demonstration application.
	 *
	 * @param args
	 *            ignored.
	 */
	/*
	 * public static void main(final String[] args) {
	 * 
	 * final LineChartDemo6 demo = new
	 * LineChartDemo6("Accumulated benifit left on deposit"); demo.pack();
	 * RefineryUtilities.centerFrameOnScreen(demo); demo.setVisible(true);
	 * 
	 * }
	 */

	private void createFrame() {

		NumberAxis xAxis = new NumberAxis("End Of Policy Year");
		xAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
		xAxis.setTickLabelFont(new Font("Georgia", Font.BOLD, 11));

		// LogAxis yAxis = new LogAxis("Value");
		NumberAxis yAxis = new NumberAxis("Value");
		LogAxis yAxis1 = new LogAxis("Value") {
			@Override
			public List refreshTicks(Graphics2D g2, AxisState state,
					Rectangle2D dataArea, RectangleEdge edge) {

				List allTicks = super.refreshTicks(g2, state, dataArea, edge);
				List myTicks = new ArrayList();
				int showCounter = 0;
				for (Object tick : allTicks) {
					NumberTick numberTick = (NumberTick) tick;
					if (TickType.MAJOR.equals(numberTick.getTickType())) {
						if (showCounter == 0) {
							showCounter = showCounter + 1;
							myTicks.add(tick);
						}
						else
						{
							showCounter=(showCounter==2)?0:showCounter + 1;
						}
					}
					System.out.println(numberTick.getValue() + "--"
							+ numberTick.getTickType());
				}
				return myTicks;
			}
		};

		
		//yAxis.setBase(1.0000000001);
		yAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
		yAxis.setTickUnit(new NumberTickUnit(250000));
		yAxis.setTickLabelsVisible(true);
		yAxis.setNumberFormatOverride(new DecimalFormat("00"));
		//yAxis.setNumberFormatOverride(NumberFormat.getInstance(Locale.US));

		// yAxis.setTickUnit(new NumberTickUnit(10000,new
		// DecimalFormat("#,###,###")));
		// yAxis.setTickUnit(new NumberTickUnit(1000, new
		// DecimalFormat("$##,##,##0")));
		yAxis.setTickLabelFont(new Font("Georgia", Font.BOLD, 11));
		final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
		renderer.setSeriesLinesVisible(0, true);
		renderer.setSeriesLinesVisible(1, true);
		renderer.setSeriesLinesVisible(2, true);

		renderer.setSeriesShapesVisible(0, false);
		renderer.setSeriesShapesVisible(1, false);
		renderer.setSeriesShapesVisible(2, false);

		renderer.setSeriesPaint(0, new java.awt.Color(51, 255, 0));
		renderer.setSeriesPaint(1, new java.awt.Color(204, 51, 51));
		renderer.setSeriesPaint(2, new java.awt.Color(51, 102, 255));

		renderer.setSeriesPositiveItemLabelPosition(0, new ItemLabelPosition(
				ItemLabelAnchor.OUTSIDE6, TextAnchor.TOP_LEFT));
		renderer.setSeriesPositiveItemLabelPosition(1, new ItemLabelPosition(
				ItemLabelAnchor.OUTSIDE3, TextAnchor.CENTER));
		renderer.setSeriesPositiveItemLabelPosition(2, new ItemLabelPosition(
				ItemLabelAnchor.OUTSIDE12, TextAnchor.BOTTOM_RIGHT));

		//XYItemLabelGenerator itemLabelGenerator = new StandardXYItemLabelGenerator(
		//		"{2}", new DecimalFormat("00"), new DecimalFormat("00"));
		XYItemLabelGenerator itemLabelGenerator = new StandardXYItemLabelGenerator(
				"{2}", new DecimalFormat("00"), NumberFormat.getInstance(Locale.US));
		

		
		//renderer.setItemLabelGenerator(itemLabelGenerator);
		renderer.setItemLabelFont(new Font("Georgia", Font.BOLD, 11));
		renderer.setBaseItemLabelsVisible(true);

		XYPlot plot = new XYPlot(createDataset(), xAxis, yAxis, renderer);
		renderer.setBaseItemLabelsVisible(true);
		renderer.setBaseItemLabelGenerator(new LegendXYItemLabelGenerator(plot
				.getLegendItems()));
		plot.getRangeAxis().setVisible(true);
		plot.setOrientation(PlotOrientation.VERTICAL);
		plot.setDomainGridlinePaint(Color.white);
		plot.setRangeGridlinePaint(Color.white);

		JFreeChart chart = new JFreeChart(
				"Accumulated benifit left on deposit",
				JFreeChart.DEFAULT_TITLE_FONT, plot, false);
		JFrame frame = new JFrame("LogAxis Test");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setContentPane(new ChartPanel(chart));
		frame.pack();
		frame.setVisible(true);
	}

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {

			@Override
			public void run() {
				final LineChartDemo6 demo = new LineChartDemo6(
						"Accumulated Benifit left on deposit");
				demo.createFrame();
			}
		});
	}

}
